package com.excellence.service.impl;

import com.excellence.mapper.ProblemMapper;
import com.excellence.pojo.Problem;
import com.excellence.service.ProblemService;
import com.excellence.utils.SaveFilesPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author 沈秉璋
 * @ClassName ProblemServiceImpl
 * @Description TODO
 * @date 2024/4/8 10:51
 * @Version 1.0
 */
@Service
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    ProblemMapper problemMapper;
    @Override
    public int deleteByKey(Long id) {
        Problem record = problemMapper.selectByPrimaryKey(id);
        record.setDeleted(1);
        return problemMapper.updateByPrimaryKey(record);
    }
    @Override
    public int deleteByPrimaryKey(Long id) {
        return problemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Problem record,  List<MultipartFile> files) {
        if (files != null) {
            record.setProbImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setCreateTime(new Date());
        record.setDeleted(0);
        return problemMapper.insert(record);
    }

    @Override
    public int insertSelective(Problem record,  List<MultipartFile> files) {
        if (files != null) {
            record.setProbImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setCreateTime(new Date());
        return problemMapper.insertSelective(record);
    }

    @Override
    public Problem selectByPrimaryKey(Long id) {
        return problemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Problem> selectAll() {
        return problemMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKeySelective(Problem record,  List<MultipartFile> files) {
        if (files != null) {
            record.setProbImage(SaveFilesPath.saveFilesPath(files));
        }
        return problemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Problem record,  List<MultipartFile> files) {
        if (files != null) {
            record.setProbImage(SaveFilesPath.saveFilesPath(files));
        }
        return problemMapper.updateByPrimaryKey(record);
    }
}
